package com.wushi.lenovo.asia5b.app_config;

import com.mvp.base.model.ResultModel;
import com.mvp.common.LocalManageUtil;
import com.mvp.dagger.modules.NetworkModule;
import com.mvp.utils.https.MapHelper;
import com.wushi.lenovo.asia5b.app_http.ApiService;
import com.wushi.lenovo.asia5b.app_model.good.ShopCarCountModel;
import com.wushi.lenovo.asia5b.app_model.other.UserInfo;
import com.wushi.lenovo.asia5b.app_push.PushHelper;

import java.util.Map;

import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.PublishSubject;
import io.reactivex.subjects.Subject;

import static timber.log.Timber.d;

/**
 * Created by zhangyuncai on 2019/11/28.
 * 用户相关静态数据
 */
public class ShopRepository {
    private final static ShopRepository shopRepository = new ShopRepository();

    private final ApiService apiService;
    private int shopCount = 0;//购物车数量
    private Subject<Integer> shopCountSubject;
    private long lastGetShopcarCountTime = 0;

    private static synchronized ShopRepository getInstance() {
        return shopRepository;
    }

    public ShopRepository() {
        shopCountSubject = PublishSubject.create();
        this.apiService = NetworkModule.getRetrofit().create(ApiService.class);
    }

    /**
     * 从后台更新数量并通知所有界面
     */
    public static void updateShopCount() {
        getInstance().getShopcarCountByNet();
    }

    public static void setShopCount(int shopCount) {
        getInstance().shopCount = shopCount;
        getInstance().shopCountSubject.onNext(shopCount);
    }

    /**
     * 订阅数据监听
     *
     * @return
     */
    public static Flowable<Integer> getShopCountOB() {
        return getInstance().shopCountSubject.toFlowable(BackpressureStrategy.LATEST);
    }

    public static int getShopCount() {
        return getInstance().shopCount;
    }

    /**
     * 获取购物车数量
     */
    private void getShopcarCountByNet() {
        if (!UserRepository.isLogin()) {
            d("--->>>>>>>>---没登录");
            return;
        }
        if ((System.currentTimeMillis() - lastGetShopcarCountTime) < 1000) {
            return;
        }
        lastGetShopcarCountTime = System.currentTimeMillis();
        Map<String, Object> map = MapHelper.newHashMap();
        map.put("member_id", UserInfo.getMemberId() + "");
        map.put("language_code", "" + LocalManageUtil.getAppLanguage());
        map.put("source", "" + 1);
        map.put("type", "daigou");
        map.put("country_id", UserInfo.getCountryId());
        map.put("session_id", PushHelper.getRegistrationId());

        apiService.getGouwucheNum(map)
                .map(new Function<ResultModel<ShopCarCountModel>, Integer>() {
                    @Override
                    public Integer apply(ResultModel<ShopCarCountModel> resultModel) throws Exception {
                        return resultModel.getData().getTotal();
                    }
                })
                .subscribeOn(Schedulers.io())
                .subscribe(total -> {
                    shopCount = total;
                    //通知所有界面
                    if (!UserRepository.isLogin()) {
                        shopCount = 0;
                    }
                    shopCountSubject.onNext(shopCount);
                }, throwable -> {
                    throwable.printStackTrace();
                });
    }
}
